const express = require('express');
const permissionController = require('../controllers/permissionController');
const { authenticateToken } = require('../middlewares/authMiddleware');
const { checkPermission ,checkSelfOrPermission,checkPermissionLevel} = require('../middlewares/permissionMiddleware');

const router = express.Router();

// 所有用户路由都需要认证Token
router.use(authenticateToken);

// 数据域类接口（通用接口，需要控制范围）

// 根据用户ID获取权限
router.get('/user/:id', checkSelfOrPermission('permission:view', { paramKey: 'id', minLevel: 20 }), permissionController.getUserPermissionCodes);


// 管理类接口（需要管理员权限）

// 获取权限列表
router.get('/',checkPermission('permission:list'),checkPermissionLevel(40),permissionController.getPermissions);

// 创建权限
router.post('/', checkPermission('permission:create'),checkPermissionLevel(40), permissionController.createPermission);

// 更新权限
router.put('/update',checkPermission('permission:update'),checkPermissionLevel(40),permissionController.updatePermission); 

// 获取权限详情
router.get('/:id',checkPermission('permission:view'),checkPermissionLevel(40),permissionController.getPermission);


// 批量删除权限
router.delete('/batch',checkPermission('permission:batch:delete'),checkPermissionLevel(40),permissionController.batchDeletePermission);

// 删除权限
router.delete('/:id',checkPermission('permission:delete'),checkPermissionLevel(40),permissionController.deletePermission);


// // 批量恢复权限
// router.post('/batch-restore', checkPermission('permission:batch:restore'), permissionController.batchRestorePermissions);

// // 硬删除过期权限（管理员维护功能）
// router.delete('/cleanup', checkPermission('permission:cleanup'), permissionController.hardDeleteOldPermissions);

// // 获取已删除的权限列表
// router.get('/deleted', checkPermission('permission:view:deleted'), permissionController.getDeletedPermissions);

// // 恢复单个权限
// router.post('/:id/restore', checkPermission('permission:restore'), permissionController.restorePermission);

// // 检查权限更新
// router.get('/checkupdate', checkPermission('permission:check:update'), permissionController.checkPermissionUpdate);

module.exports = router;